package com.jn.viewtest_2.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jn.viewtest_2.R;

public class HeaderView extends RelativeLayout {
    private TextView mTitle;
    private Button mButtonLeft;
    private Button mButtonRight;
    public HeaderView(Context context) {
        super(context);
        initView();
    }

    public HeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HeaderView);
        String title = typedArray.getString(R.styleable.HeaderView_text);
        setTitle(title);
        String leftStr = typedArray.getString(R.styleable.HeaderView_left_text);
        setLeftText(leftStr);
        String rightStr = typedArray.getString(R.styleable.HeaderView_right_text);
        setRightText(rightStr);
        Drawable leftDrawable = typedArray.getDrawable(R.styleable.HeaderView_left_background);
        if(leftDrawable != null) {
            mButtonLeft.setBackground(leftDrawable);
        } else {
            int color = typedArray.getColor(R.styleable.HeaderView_left_background, 0xffffff00);
            mButtonLeft.setBackgroundColor(color);
        }
        Drawable rightDrawable = typedArray.getDrawable(R.styleable.HeaderView_right_background_drawable);
        if (rightDrawable != null) {
            mButtonRight.setBackground(rightDrawable);
        } else {
            int rightColor = typedArray.getColor(R.styleable.HeaderView_right_background_color, 0xffffff00);
            mButtonRight.setBackgroundColor(rightColor);
        }
        int gravity = typedArray.getInt(R.styleable.HeaderView_gravity, 0);
        LayoutParams layoutParams = (LayoutParams) mButtonLeft.getLayoutParams();
        if(gravity == 0) {
            layoutParams.addRule(CENTER_VERTICAL);
        } else if (gravity == 1) {
            layoutParams.addRule(ALIGN_PARENT_TOP);
        } else if (gravity == 2) {
            layoutParams.addRule(ALIGN_PARENT_BOTTOM);
        }
        mButtonLeft.setLayoutParams(layoutParams);
        typedArray.recycle();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.header_view, this, true);
        mTitle = findViewById(R.id.title);
        mButtonLeft = findViewById(R.id.button_left);
        mButtonRight = findViewById(R.id.button_right);
    }

    public void setTitle(String title) {
        if (mTitle != null && !TextUtils.isEmpty(title)) {
            mTitle.setText(title);
        }
    }

    public void setLeftText(String leftStr) {
        if (mButtonLeft != null && !TextUtils.isEmpty(leftStr)) {
            mButtonLeft.setText(leftStr);
        }
    }

    public void setRightText(String rightStr) {
        if (mButtonRight != null && !TextUtils.isEmpty(rightStr)) {
            mButtonRight.setText(rightStr);
        }
    }

    public void setLeftClickListener(OnClickListener listener) {
        if (mButtonLeft != null && listener != null) {
            mButtonLeft.setOnClickListener(listener);
        }
    }

    public void setRightClickListener(OnClickListener listener) {
        if (mButtonRight != null && listener != null) {
            mButtonRight.setOnClickListener(listener);
        }
    }

    public void setLeftVisibility(int visibility) {
        if (mButtonLeft != null) {
            mButtonLeft.setVisibility(visibility);
        }
    }

    public void setRightVisibility(int visibility) {
        if (mButtonRight != null) {
            mButtonRight.setVisibility(visibility);
        }
    }

    public void setLeftBackground(int color) {
        if (mButtonLeft != null) {
            mButtonLeft.setBackgroundColor(color);
        }
    }
}